﻿using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IPermissionService;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Domain.DomainService.System.PermissionService
{
    public class OperationManagementService : IOperationManagementService
    {
        private IRepository<AppOperation> OperationRep;

        public OperationManagementService(IRepository<AppOperation> OperationRep)
        {
            this.OperationRep = OperationRep;
        } 
        public async Task<DomainResult<AppOperation>> CreateOperation(AppOperation appOperation)
        {
            var user = OperationRep.Table.FirstOrDefault(w => w.OperationName == appOperation.OperationName);
            
            if (user != null)
            {
                return DomainResult<AppOperation>.Error("操作权限已存在");
            }
            var operation = await OperationRep.CreateAsync(appOperation);

            return DomainResult<AppOperation>.Success(operation);
        }

        public async Task<DomainResult<AppOperation>> DeleteOperation(Guid operId)
        {
            var permission = await OperationRep.DeleteAsync(operId);
            if (permission == null)
            {
                return DomainResult<AppOperation>.Error("操作权限不存在");
            }
            return DomainResult<AppOperation>.Success(permission);
        }

        public async Task<DomainResult<List<AppOperation>>> GetOperationWithPagination(int page, int pageSize)
        {
            var OperationList = await OperationRep.GetListAsync();

            if (OperationList.Count == 0)
            {
                return DomainResult<List<AppOperation>>.Error("当前还没有任何用户");
            }
            var OperationListPage = OperationList.Skip((page - 1) * pageSize).Take(pageSize).Where(item => item.IsDeleted == false).ToList();

            return DomainResult<List<AppOperation>>.Success(OperationListPage);
        }

        public async Task<DomainResult<AppOperation>> GetOperationById(Guid id)
        {
            var permission = await OperationRep.GetByIdAsync(id);
            if (permission == null)
            {
                return DomainResult<AppOperation>.Error("操作权限不存在");
            }
            return DomainResult<AppOperation>.Success(permission);
        }

        public async Task<DomainResult<AppOperation>> UpdateOperationProfile(Guid Id, AppOperation entity)
        {
            var operation = await OperationRep.GetByIdAsync(Id);
            if (operation == null) {
                return DomainResult<AppOperation>.Error("操作权限不存在");
            }

            operation.OperationName = entity.OperationName;
            operation.Descript = entity.Descript;

            await OperationRep.UpdateAsync(operation);

            return DomainResult<AppOperation>.Success(operation);

        }
    }
}
